﻿Imports System.Text.RegularExpressions
Public Class EnglistTestWord

    Private cls(36) As Wys.Control.Vb.WordTest

#Region "只能入力数字 "
    Private Sub txtPage_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbo页数.KeyPress
        '应许输入退格键 C# 强制转换
        If Asc(e.KeyChar) <> 8 Then
            '正则验证数字
            If (Not Regex.IsMatch(e.KeyChar.ToString(), "^[0-9]$")) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub
#End Region

#Region "页数"
    Private _TotalPage As Integer = 0
    Public Property TotalPage() As Integer
        Get
            Return _TotalPage
        End Get
        Set(ByVal value As Integer)
            _TotalPage = value
        End Set
    End Property

    Private _CurrentPage As Integer = 0
    Public Property CurrentPage() As Integer
        Get
            Return _CurrentPage
        End Get
        Set(ByVal value As Integer)
            _CurrentPage = value
        End Set
    End Property
#End Region

#Region "设置记录条数"
    Private _CurrentTable As DataTable = Nothing
    Public Sub RefreshRecordSet()
        ClearForm()
        Dim strSql As String = "select ID,WORD,CHINESEWORD,SyombalAmerican from XSCENLISHWORD"
        strSql &= " ORDER BY "
        If chk浏览次数.Checked Then
            strSql &= "Count Desc "
            strSql &= ",OPTIME DESC"
        Else
            strSql &= "OPTIME DESC"
        End If
        strSql &= " Limit " + Me.Get窗体行数().ToString + " Offset " & (Me.Get窗体行数() * (CurrentPage - 1)).ToString
        _CurrentTable = ct.FillTable(ct.GetTable(strSql))

        For i As Integer = 0 To cls.Length - 1
            If _CurrentTable.Rows.Count > 0 Then
                cls(i).单词意思 = Convert.ToString(_CurrentTable.Rows(0)("CHINESEWORD"))
                cls(i).单词 = Convert.ToString(_CurrentTable.Rows(0)("WORD"))
                cls(i).单词发音 = Convert.ToString(_CurrentTable.Rows(0)("SyombalAmerican"))
                _CurrentTable.Rows.RemoveAt(0)
                cls(i).Enabled = True
            Else
                Exit For
            End If
        Next
        cls(0).Focus()
    End Sub
#End Region

    Public Sub ClearForm()
        For i As Integer = 0 To cls.Length - 1
            Try
                cls(i).单词 = ""
                cls(i).单词意思 = ""
            Catch ex As Exception

            End Try
        Next
    End Sub

    Private Sub EnglistTestWord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim cls(Me.Get窗体行数())

        For i As Integer = 1 To (cls.Length) / 3
            For j As Integer = 1 To 3
                If (i - 1) * 3 + j > Me.Get窗体行数 Then
                    GoTo 退出
                End If
                cls((i - 1) * 3 + j - 1) = New Wys.Control.Vb.WordTest()
                cls((i - 1) * 3 + j - 1).Left = 8 + 241 * (j - 1) + 5 * (j - 1)
                cls((i - 1) * 3 + j - 1).Top = 101 + 29 * (i - 1) + 6 * (i - 1)
                cls((i - 1) * 3 + j - 1).ImeMode = Windows.Forms.ImeMode.Off
                AddHandler cls((i - 1) * 3 + j - 1).RefreshTitle, AddressOf WordTest36_RefreshTitle
                Me.Controls.Add(cls((i - 1) * 3 + j - 1))
            Next
        Next

退出:
        Dim rs As ADODB.Recordset = ct.GetTable("select count(*) as PageCount from XSCENLISHWORD")
        TotalPage = System.Math.Ceiling((rs.Fields("PageCount").Value) / Me.Get窗体行数())
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        rs = Nothing
        For idex As Integer = 1 To TotalPage
            cbo页数.Items.Add(idex.ToString())
        Next

        Me.txt发音.Font = New Drawing.Font("Lucida Sans Unicode", Me.MyEdit1.Font.Size)

        If CommonModule.是否从窗口调用英语测试程序 = True Then
            Me.chk浏览次数.Checked = CommonModule.是否为按浏览次数
            Me.cbo页数.Text = CommonModule.当前页数.ToString
        Else
            Me.cbo页数.Text = Me._currentText
        End If
    End Sub

    Private Sub WordTest36_RefreshTitle(ByVal strTitle As System.String, ByVal strFa As String)
        MyEdit1.Text = strTitle
        txt发音.Text = strFa
        RefrshState()
    End Sub

    Private Sub cbo页数_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo页数.SelectedIndexChanged
        CurrentPage = CInt(Me.cbo页数.Text)
        RefreshRecordSet()
    End Sub

    Public Sub RefrshState()
        Dim count As Integer = 0
        For Each c2 As Wys.Control.Vb.WordTest In cls
            If c2 IsNot Nothing Then
                If c2.选中状态 Then
                    count = count + 1
                End If
            End If
        Next
        Me.txt状态.Text = "正确:" + count.ToString + "/共:" & Me.Get窗体行数().ToString
    End Sub

    Private _开始学习 As Boolean = False
    Private Sub btn开始学习_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始学习.Click

        If btn开始学习.Text = "开始学习" Then
            btn开始学习.Text = "停止学习"
            _开始学习 = True
            For Each c2 As Wys.Control.Vb.WordTest In cls
                If _开始学习 Then
                    If c2 IsNot Nothing Then
                        If c2.单词意思数据库.Trim <> "" Then
                            c2.Focus()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            c2.单词意思数据库 = ""
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                        Else
                            Exit For
                        End If
                    Else
                        Exit For
                    End If
                End If
            Next
            btn开始学习.Text = "开始学习"
            _开始学习 = False
        Else
            btn开始学习.Text = "开始学习"
            _开始学习 = False
        End If
    End Sub

    Private _开始浏览 As Boolean = False
    Private Sub btn开始浏览_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始浏览.Click
        If btn开始浏览.Text = "开始浏览" Then
            btn开始浏览.Text = "停止浏览"
            _开始浏览 = True
            For Each c2 As Wys.Control.Vb.WordTest In cls
                If _开始浏览 Then
                    If c2 IsNot Nothing Then
                        If c2.单词意思数据库.Trim <> "" Then
                            c2.Focus()
                            c2.单词意思数据库 = c2.单词意思数据库
                            System.Windows.Forms.Application.DoEvents()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                        Else
                            Exit For
                        End If
                    Else
                        Exit For
                    End If
                End If
            Next
            btn开始浏览.Text = "开始浏览"
            _开始浏览 = False
        Else
            btn开始浏览.Text = "开始浏览"
            _开始浏览 = False
        End If
    End Sub

    Public Sub 朗读单词(ByVal strWord As String)
        Dim strSQL As String = "select * from XSCWORDVoice where WORD='" + strWord + "'"
        Dim rs As ADODB.Recordset = ct.ExecuteSql(strSQL)
        If rs IsNot Nothing AndAlso Not rs.EOF Then
            UTIL.播放类.Play(rs.Fields("WORDPATH").Value.ToString())
            rs.Close()
        End If
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
    End Sub

#Region "重载设置窗体函数 A-2010-09-02"
    Public Overrides Function 初始行数() As Integer
        Return 20
    End Function

    Public Overloads Function Get窗体行数() As Integer
        Return MyBase.Get窗体行数(初始行数())
    End Function

    Public Overrides Function 是否显示设置窗体数据行数() As Boolean
        Return True
    End Function
#End Region

#Region "窗体数据保存的重载方法 A-2010-09-06"
    Public Overrides Sub 保存窗体数据到数据库(ByRef dtData As System.Data.DataTable)
        If chk浏览次数.Checked Then
            dtData.Rows.Add(New Object() {"按浏览次数排序", "", "选中"})
        Else
            dtData.Rows.Add(New Object() {"按浏览次数排序", "", "不选中"})
        End If
        dtData.Rows.Add(New Object() {"页数", "", Me.cbo页数.Text})
    End Sub

    Public Overrides Sub 存取数据库数据到窗体(ByVal dtDbData As System.Data.DataTable)
        If dtDbData.Rows.Count > 0 Then
            If 查找数据("按浏览次数排序", "", "不选中", dtDbData) = "选中" Then
                chk浏览次数.Checked = True
            Else
                chk浏览次数.Checked = False
            End If
            Me.cbo页数.Text = 查找数据("页数", "", "1", dtDbData)
            _currentText = 查找数据("页数", "", "1", dtDbData)
        End If
    End Sub

    Private _currentText As String = ""
#End Region

    Private Sub btn详细画面_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn详细画面.Click
        If Me.cbo页数.Text.Trim = "" Then
            ShowMessageBox("请选择页数")
            Exit Sub
        End If

        CommonModule.是否从窗口调用英语浏览程序 = True
        CommonModule.当前页数 = Convert.ToInt32(Me.cbo页数.Text)
        CommonModule.是否为按浏览次数 = Me.chk浏览次数.Checked
        Using frm As ListEnglishWord = New ListEnglishWord
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()
        End Using
        CommonModule.是否从窗口调用英语浏览程序 = False
    End Sub
End Class
